<template>
  <div class="dep_main">
   <!-- <h1>部门管理</h1> -->
    <div class="btn_div">
      <Power url="/dep/insert">
        <el-button type="success" style="background-color: #42B983;" @click="dialogFormVisible = true">新增部门</el-button>
      </Power>
    </div>

    <!-- 自定义的表格组件 -->
    <Table ref="mytable" :param="table"></Table>

    <!-- 新增部门的弹窗 -->
    <el-dialog title="新增部门" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="部门名称" :label-width="formLabelWidth">
          <el-input v-model="form.depName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="insertDep()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import Table from './Table'

  export default{
    components:{
      Table
    },
    data(){
      return {
        formLabelWidth: '120px',
        //表单数据
        form: {
          depName: ""
        },
        //弹出框的显示标识
        dialogFormVisible: false,
        //表格组件的属性
        table: {
          title: [
            {name: "部门编号", col:"id"},
            {name: "部门名称", col:"depName"},
            {name: "部门人数", col:"depNum"},
            {name: "成立时间", col:"createTime"}
          ],
          url: this.$.url.deplist,
          page: {
            enable: true, //是否开启表格组件的分页
            pageSize: 5 //每页显示多少条
          },
          btns: [
            {
              btnName: "编辑",
              click: function(rowData){
                console.log("编辑：" + JSON.stringify(rowData));
              }
            },
            {
              btnName: "删除",
              click: function(rowData){
                console.log("删除：" + JSON.stringify(rowData));
              }
            }
          ]
        }
      }
    },
    methods:{
      //新增部门
      insertDep(){
        //1、先发ajax
        this.$.ajax({
          type: "POST",
          url: this.$.url.depinsert,
          data: this.form,
          success: (data) => {
            if(data.code == 200){
               //新增成功
               //2、关闭弹窗
               this.dialogFormVisible = false;
               //3、刷新表格
               this.$refs.mytable.request();
            } else if(data.code == 407){
               //数据已经存在
               this.$message.error('部门已经存在');
            } else if(data.code == 405){
               //参数校验异常
               var error = data.data;
               //获取当前对象中的所有value
               for(var er in error){
                 var info = error[er]
                 this.$message.error(info);
               }
            }
          }
        });
      }
    },
    mounted(){

    }
  }
</script>

<style>
  .dep_main {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    height: 100%;
    width: 100%;
  }

  .table_cls {
    margin-top: 10px;
  }

  .btn_div {
    /* margin-top: 50px; */
    background-color: #E9EEF3;
    text-align: left;
    width: 100%;
    height: 60px;
    display: flex;
    flex-direction: row;
    justify-content: left;
    align-items: center;
    padding-left: 50px;
  }
  
  .btn_div > * {
    margin-right: 10px;
  }
</style>
